package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.GroupCommentDAO;
import com.wisewin.circle.dao.TrendCommentDAO;
import com.wisewin.circle.dao.TrendDAO;
import com.wisewin.circle.dao.UserReportDAO;
import com.wisewin.circle.entity.db.GroupCommentDO;
import com.wisewin.circle.entity.db.TrendCommentDO;
import com.wisewin.circle.entity.db.TrendDO;
import com.wisewin.circle.entity.db.UserReportDO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserReportService {

    @Autowired
    private UserReportDAO userReportDAO;
    @Autowired
    private TrendDAO trendDAO;
    @Autowired
    private TrendCommentDAO trendCommentDAO;
    @Autowired
    private GroupCommentDAO groupCommentDAO;

    // 添加举报
    public ResultDTO addUserReport(UserReportDO userReportDO){
        if(userReportDO.getDataId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        if(StringUtils.isEmpty(userReportDO.getContent())){
            return ResultDTOBuilder.failure("1", "请填写举报信息", null);
        }
        // 举报数据类型  0：动态 1：动态评论 2：拼团评论
        if(userReportDO.getDataType() == 0){
            TrendDO trendDO = trendDAO.queryTrendById(userReportDO.getDataId());
            if(trendDO == null){
                return ResultDTOBuilder.failure("1", "数据不存在", null);
            }
        }else if(userReportDO.getDataType() == 1){
            TrendCommentDO trendCommentDO = trendCommentDAO.queryTrendCommentById(userReportDO.getDataId());
            if(trendCommentDO == null){
                return ResultDTOBuilder.failure("1", "数据不存在", null);
            }
        }else if(userReportDO.getDataType() == 2){
            GroupCommentDO groupCommentDO = groupCommentDAO.queryGroupCommentById(userReportDO.getDataId());
            if(groupCommentDO == null){
                return ResultDTOBuilder.failure("1", "数据不存在", null);
            }
        }
        userReportDAO.insert(userReportDO);
        return ResultDTOBuilder.success(userReportDO);
    }
}
